#!/usr/bin/env bun
import retry from '@3-/retry'
import { JINA_URL, JINA_API_KEY, JINA_MODEL_NAME, JINA_EMBEDDING_DIMENSIONS } from './conf.js';

const jina = (task)=>retry(async (li) => {
  const response = await fetch(JINA_URL, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${JINA_API_KEY}`,
    },
    body: JSON.stringify({
      input: li,
      model: JINA_MODEL_NAME,
      dimensions: JINA_EMBEDDING_DIMENSIONS,
      task, 
      late_chunking: true,   // 启用 Late Chunking
    }),
  });
  const { data } = await response.json();
  return data.map(item => item.embedding);
})

export const jinaIndex = jina('retrieval.passage')
const _jinaQuery = jina('retrieval.query')
export const jinaQuery = async (query) => (await _jinaQuery([query]))[0]

